<template>
	<div>
		<!-- 富文本框的容器 -->
		<!-- id 使用动态id,解决切换加载需要刷新问题 -->
		<textarea :id="tinymceId"></textarea>
		<el-button type="primary" size="default" @click="bindConfirm">确定</el-button>
	</div>
</template>

<script>
import { ElMessage } from 'element-plus'
import { api } from '@/api'
import { getUserFromStore } from '@/common/useStore'
const { RequestLogAdd } = api
export default {
	computed: {
		tinymceId() {
			return 'vue-tinymce-' + Date.now()
		}
	},
	mounted() {
		this.$nextTick(() => {
			// eslint-disable-next-line no-undef
			tinymce.init({
				selector: '#' + this.tinymceId,
				branding: false,
				height: 600,
				auto_focus: true,
				plugins: 'advlist link image lists paste',
				paste_data_images: true //支持图片粘贴
			})
		})
	},
	methods: {
		bindConfirm() {
			this.bindAddLog()
		},
		async bindAddLog() {
			// eslint-disable-next-line no-undef
			const content = tinymce.activeEditor.getContent() // 内容
			const account = getUserFromStore()
			const mid = account.mid // 操作员id
			console.log('mid ',mid);
			const category = 1 // 富文本
			const data = await RequestLogAdd(mid, content, category)
			const { resultCode } = data
			if (resultCode === 1) {
				ElMessage.success('添加成功!')
                // eslint-disable-next-line no-undef
                tinymce.activeEditor.setContent('')
			}
		}
	}
}
</script>

<style lang="scss" scoped></style>
